package com.ruoyi.airspaceFlight.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.airspaceFlight.domain.TAirspaceSettings;
import com.ruoyi.airspaceFlight.domain.TAirspaceType;
import com.ruoyi.airspaceFlight.mapper.TAirspaceSettingsMapper;
import com.ruoyi.airspaceFlight.mapper.TAirspaceTypeMapper;
import com.ruoyi.airspaceFlight.service.TAirspaceTypeService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author SUNLIANG
 * @description 针对表【t_airspace_type(空域类型表)】的数据库操作Service实现
 * @createDate 2023-11-01 16:58:39
 */
@Service
public class TAirspaceTypeServiceImpl extends ServiceImpl<TAirspaceTypeMapper, TAirspaceType>
        implements TAirspaceTypeService {

    @Autowired
    private TAirspaceTypeMapper tAirspaceTypeMapper;

    @Autowired
    private TAirspaceSettingsMapper tAirspaceSettingsMapper;

    @Override
    public List<TAirspaceType> selectTAirspaceTypeList() {

        return tAirspaceTypeMapper.selectTAirspaceTypeList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTAirspaceType(TAirspaceType tAirspaceType) {

        tAirspaceType.setCreateTime(DateUtils.getNowDate());
        tAirspaceType.setCreateBy(SecurityUtils.getUsername());
        tAirspaceType.setUpdateTime(DateUtils.getNowDate());
        tAirspaceType.setUpdateBy(SecurityUtils.getUsername());

        return tAirspaceTypeMapper.insert(tAirspaceType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTAirspaceType(TAirspaceType tAirspaceType) {
        tAirspaceType.setUpdateTime(DateUtils.getNowDate());
        tAirspaceType.setUpdateBy(SecurityUtils.getUsername());

        return tAirspaceTypeMapper.update(tAirspaceType, new LambdaQueryWrapper<TAirspaceType>()
                .eq(TAirspaceType::getAirTypeId, tAirspaceType.getAirTypeId()));
    }

    @Override
    public TAirspaceType selectTAirspaceTypeByAirTypeId(String airTypeId) {

        return tAirspaceTypeMapper.selectOne(new LambdaQueryWrapper<TAirspaceType>().eq(TAirspaceType::getAirTypeId, airTypeId));

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTAirspaceTypeByAirTypeId(String airTypeId) {

        Integer integer = tAirspaceSettingsMapper.selectCount(new LambdaQueryWrapper<TAirspaceSettings>().eq(TAirspaceSettings::getAirTypeId, airTypeId));
        //判断是否被应引用
        if (integer > 0){
            return 0;
        }

        return tAirspaceTypeMapper.delete(new LambdaQueryWrapper<TAirspaceType>().eq(TAirspaceType::getAirTypeId, airTypeId));
    }
}




